[Overall][For...Next][Do...Loop][Mid Statement] [Opening files][Writing to files[Reading from files][Pre-Defined Functions] Trapping Errors |
What's so important about the basics? | |
The basics of VB is the most important part of learning how to program. If you don't know the basics i can assure you that it wil be very hard to learn the advanced methods. Too many times i find myself explaining to 2 and 3 year long programmers things that they should have learned the first month. I never realized this problem untill people started to recognize me, the more they did the more people would ask me for help. It became harder and harder for me to help them because i wasn't able to just tell them to make a For...Next statement, because all too often what i would get was "Whats that?". This is fine...for a programmer who has been studying it for a month or 2 maybe even 3, but for 2 years? So without wasting anymore time, here are the basics: | |
For...Next Statement: This is a way of looping, the syntax is as fallows: Dim i as integer For i = 1 to 10 Msgbox(i) Next i What this will do is make a messagebox pop up one at a time and the message will be 1 then 2 and so on.. For...Next-Steps: You can use steps to control how the numbers will be counted Ex: Dim i as integer For i = 1 to 10 Step 2 Msgbox(i) Next i What this will do is bring the message up one at a time, but this time the numbers will skip by 2's like this, 2, 4, 6, 8, 10 Use of Step -1 will cause the number to go backwards. If i = 100 to 1 Step -1, then the numbers will count back to 1. For...Next-Exiting: You don't have to have the statement count all the way to the end. Let's say you wanted to try something 5 times, if the condition wasn't met after 5 times you'd exit the program. But if it did happen you want it to do something else. Here's an example: Dim i as integer Dim PassW as Boolean PassW = False For i = 1 to 5 inp = InputBox("Password","You have " & i & " tries left.") If inp = Password then PassW= True Exit For End if Next i If PassW = True then 'They got the password Else 'They didnt, kick em out! End if Do Loop: This is another type of loop, Most people know this one though. The syntax is: ��Dim i as integer ��i=0 ��Do:DoEvents ��MsgBox(i) ��i = Val(i)+1 ��If i = 5 then Exit Do ��Loop If...Then: We all know this one but i will show you a way to shorten them. A lot of people wont use Case so ill show this way: If x <> 0 then Msgbox("X isnt zero") Elseif x < 0 then Msgbox("X is less than zero") Elseif x > 5 then Msgbox("X is greater then 5") End if If you have a lot of if..then's, this will help. < a name="select">Select Case: another way to substitute for If...Then's Dim i as integer i = int(rnd*3)+1 �� Select Case of i ����Case 1 ��msgbox('# is 1') ����Case 2 ��msgbox('# is 2') ����Case 3 ��msgbox('# is 3') �End Select Mid Statement: This will return parts of a string. The syntax is as fallows: i$ = Mid(String,Where to start,How many characters to take) So: S = "Hound_Dog" ��Mid(S,1,3) will = "Hou" Combine this with a for next statement and you can make really nice text manipulation functions | |
Opening Files | |
Opening files is easier than most programmers think. If you want more help than i give you here then look up "Open Statement" in the VB Help file. Since 80% of programmers i see each day don't have a help file, i decided to teach it here. Syntax Open FilePath for (Mode) (access)as (filenumber) FilePath = a string expression Ex. "c:\windows\desktop\whatever.txt" Mode = The mode you want to open the file in: Output = Write to file Input = Get data from file Binary = Open file to read binary data Append = Open file to write data starting from end of file Random = Basically just open the file for use Access = What type of access you want Read Write ReadWrite FileNumber = Each file has to be assigned a numer in range 1-511 What you can do is set a variable to = FreeFile. Then open up as that variable. Ex: FreeNum = FreeFile Open FilePath for Binary Access Read as FreeNum | |
Writing to files | |
Writing to a file is just as easy. You can use the Print# or Write# statement, i'll show Print# for this Open "c:\test.txt" For Output As #1 ��Print #1,"This is a test for writing data to a txt file.." Close #1 Thats pretty basic and self explanatory so i'll move on to Input from files | |
Reading data from files | |
Reading data is a little bit more tricky. Take a look at this: Open FileName For Input As #1 ��I = FileLen(FileName) ����x = Input(I - 2, #1) Close #1 ��datainputed = x First we opened the file as a FreeFile, then found out the Len (Length) of the file. I subtracted 2 from the Len because when input is gotten through "input" and not "input#" then 2 "junk" characters will be added. X is a variable set to hold the data that we input, and we used the Input function here You can also use Input#, but i'll show this for now. Input has a syntax as fallows: Variable = Input(Number of characters,Which File #) Simple enough but the hard part about opening files is that there's a lot of ways to do it. Try to get your hands on a help file in order to fully understand them all. Trapping/Avoiding errors there are a lot of ways to avoid or trap errors aside from just using On Error Resume Next, here are two common ways: Trap error with On Error Resume Next: *This code contains an error to show how it works. On Error Resume Next Open "c:\NoSuchDir\wrongfile.idh" for input as #1 Close #1 If Err then MsgBox("Error:" & Err.Number & " " & Err.Description),16,("Error occurred") exit sub End if Trap error with On Error GoTo: *This code contains an error to show how it works. On Error GoTo Catch Dim i as integer i = "error will occur, since i declared as integer and setting as string" if i = 5 then 'doesnt matter program will skip this because of error End if Catch: MsgBox("Error:" & Err.Number & " " & Err.Description),16,("Error occurred") End Sub | |
Usefull Phrases: These are usefull functions that will help you in programming. | |
DoEvents | Basically tells VB to get off its ass and do it. Wont make a big difference, but if your program wont perform a certain task or freezes for no reason, then add this in anywhere you want. |
On Error Resume Next | Tells VB that in the event of an error, to NOT close the program but instead to just resume it at the next line. You can also use On Error GoTo --- , But GoTo statements aren't reliable in any language. |
IsNumeric | This is a Boolean Function (Boolean means that it'll either return a True or False). This will test to see if a string is a number or not Ex: 1234 -Would = True 1234B-Would = False |
LCase | Turns string into lowercase. |
UCase | Turns string into Uppercase. |
Chr | Returns character for character code entered Ex: Chr(34) = " |
Int | Changes type to integer to avoid #'s such as 1.0504053. |
Rnd | Can be used to create a random number. int(Rnd*50)+1 would make a random # from 1 to 50 |
Hex | Returns Hex value of number(s). |